--[[
	游戏数据日志， 如果是单独写钻石的话，放在 app/diamond 下，通常目录在 applog/gamedata下
	文件按照游戏节点（或者自定义文件名），日期分隔
]]
local filename = "datalog.lua"

local skynet = require "skynet"
require "skynet.manager"

local tabletool = require "tabletool"
local timetool = require "timetool"
local redisdao = require "redisdao"
local os = os

local json = require "cjson"
json.encode_sparse_array(true,1,1)

local routerbase = require "routerbase"

local CMD = {}
local dirname = ""
local filename = ""

local Gamedatalog = routerbase:new()

local function get_file_name()
	 ----- 日志分日期存储------
	local log_path = skynet.getenv("logpath") .. "/" .. dirname
	return log_path.."/" .. filename .. "_".. os.date("%Y_%m_%d", timetool.get_time()) .. ".log"
end

local function write_log(file, str)
	local f = io.open(file, "a+")
	if f ~= nil then
		f:write(os.date("%Y-%m-%d %X", timetool.get_time()) .. "," .. str .. "\n")
		f:close()
	end	
end

-- 写入log文件，并且写入到redis里面，推送到推广员系统中
local function write_log_redis(file, str, rid)
	local f = io.open(file, "a+")
	now = timetool.get_time()
	if f ~= nil then
		f:write(os.date("%Y-%m-%d %X", now) .. "," .. str .. "\n")
		f:close()
	end
	local cmd = {msghead={msgname="game_fuwufei"}, msgbody={rid=rid, logstr=now .. "," ..str}}
	redisdao.query_data(".gmmqredis", "rpush", "outfwfmq", json.encode(cmd))
end

-- 写入log文件，并且写入到redis里面，推送到后台
local function withdraw_write_log_redis(file, str, rid)
	local f = io.open(file, "a+")
	now = timetool.get_time()
	if f ~= nil then
		f:write(os.date("%Y-%m-%d %X", now) .. "," .. str .. "\n")
		f:close()
	end
	local cmd = {msghead={msgname="game_withdraw"}, msgbody={rid=rid, logstr=now .. "," ..str}}
	redisdao.query_data(".gmmqredis", "rpush", "outwithdrawmq", json.encode(cmd))
end


local function write_jackpot_log_redis(file, str, rid)
	local f = io.open(file, "a+")
	now = timetool.get_time()
	if f ~= nil then
		f:write(os.date("%Y-%m-%d %X", now) .. "," .. str .. "\n")
		f:close()
	end
	local cmd = {msghead={msgname="game_jackpot"}, msgbody={rid=rid, logstr=now .. "," ..str}}
	redisdao.query_data(".gmmqredis", "rpush", "outjackpotplayermq", json.encode(cmd))
end

local function jackpot_write_log_redis(file, str, jackpot_rid)
	local f = io.open(file, "a+")
	now = timetool.get_time()
	if f ~= nil then
		f:write(os.date("%Y-%m-%d %X", now) .. "," .. str .. "\n")
		f:close()
	end
	local cmd = {msghead={msgname="game_jackpot"}, msgbody={ jackpot_rid = jackpot_rid, logstr=now .. "," ..str}}
	redisdao.query_data(".gmmqredis", "rpush", "outjackpotmq", json.encode(cmd))
end

-- 每次启动的时候，检查 path/xxxxx 是否建立好
-- 还可以初始化文件名
function Gamedatalog.CMD.init(dir, file)
	dirname = dir or "game"
	filename = file or skynet.getenv("nodename")
	local log_path = skynet.getenv("logpath") .. "/" .. dirname
	local file = io.open(log_path, "rb")
	if file then
		file:close()
	else
		os.execute("mkdir -p "..log_path)
	end
end

function Gamedatalog.CMD.write(str)
	local file = get_file_name()
	write_log(file, str)
end

-- 写入文件并且发送到redis里面
function Gamedatalog.CMD.write_file_redis(str, rid)
	local file = get_file_name()
	write_log_redis(file, str, rid)
end

function Gamedatalog.CMD.write_player_jackpot_redis(str, rid)
	local file = get_file_name()
	write_jackpot_log_redis(file, str, rid)
end

-- 奖池日志文件写入
function Gamedatalog.CMD.jackpot_write_file_redis(str, jackpot_rid)
	local file = get_file_name()
	jackpot_write_log_redis(file, str, jackpot_rid)
end

-- 提现记录日志并发送到redis
function Gamedatalog.CMD.withdraw_write_file_redis(str, rid)
	local file = get_file_name()
	withdraw_write_log_redis(file, str, rid)
end

skynet.start(function()
	Gamedatalog:start()
end)
